function t(t) {
    var e = t.split("-");
    return new Date(e[0], e[1] - 1, e[2], 0, 0, 0).getTime() / 1e3;
}

function e(t) {
    var e = t.split(":");
    return 60 * e[0] * 60 + 60 * e[1];
}

function a(t) {
    return !t || "undefined" == t || 0 == t.trim().length;
}

function o(t) {
    return t * Math.PI / 180;
}

function n(t) {
    wx.showLoading ? wx.showLoading({
        title: t,
        mask: !0
    }) : wx.showToast({
        title: t,
        icon: "loading",
        mask: !0,
        duration: 2e4
    });
}

function s() {
    wx.hideLoading ? wx.hideLoading() : wx.hideToast();
}

function r(t, e) {
    var o = a(e) ? t : t + " " + e;
    wx.showModal({
        title: "提示",
        content: o,
        showCancel: !1,
        confirmText: "我知道了",
        confirmColor: "#ff5353"
    });
}

function i(t, e) {
    wx.showModal({
        title: e || "提示",
        content: t,
        showCancel: !1,
        confirmText: "我知道了",
        confirmColor: "#ff5353"
    });
}

function c(t, e, a) {
    if ((a && a.errMsg || "").indexOf("fail") >= 0) s(); else {
        var o = a && a.userInfo || h("userInfo");
        if (o.nickName || "") {
            var n = h("code");
            p("userInfo", o), p("authButtonClicked", !0), wx.request({
                url: getApp().globalData.host + "/lotto/v1/login_new",
                data: {
                    code: n,
                    user_data: a || {}
                },
                method: "POST",
                success: function(o) {
                    if (s(), o.data.data && o.data.data.access_token) {
                        var n = o.data.data.uname, r = o.data.data.upic, i = o.data.data.phone;
                        wx.setStorage({
                            key: "accessToken",
                            data: o.data.data.access_token,
                            success: function(e) {
                                "function" == typeof t && t(), p("userInfo", {
                                    nickName: n,
                                    avatarUrl: r,
                                    phone: i
                                });
                            },
                            fail: function(e) {
                                k.globalData.accessToken = e.data.data.access_token, k.globalData.userInfo = {
                                    nickName: n,
                                    avatarUrl: r,
                                    phone: i
                                }, "function" == typeof t && t();
                            }
                        });
                    } else if (-1 == o.data.sta && 1e3 == o.data.errcode) wx.navigateTo({
                        url: "/pages/exception/exception"
                    }); else {
                        var u = o.data.msg;
                        if (h("loginNum") && h("loginNum") > 1) return;
                        u && (u.indexOf("40163") > 0 || u.indexOf("40029") > 0) && wx.login({
                            success: function(o) {
                                var n = o.code, s = h("loginNum") || 0;
                                s++, p("code", n), p("loginNum", s), c(t, e, a);
                            }
                        }), "function" == typeof e && e();
                    }
                },
                fail: function(t) {
                    r("用户登录超时，" + t.errMsg), "function" == typeof e && e();
                }
            });
        }
    }
}

function u(t, e) {
    k.login.cbSuccess.push(t), k.login.cbError.push(e), 1 != k.login.status && (k.login.status = 1, 
    n("正在登录"), wx.login({
        success: function(t) {
            var a = t.code;
            wx.request({
                url: getApp().globalData.host + "/lotto/v1/login_new",
                data: {
                    code: a,
                    user_data: {}
                },
                method: "POST",
                success: function(t) {
                    if (t.data.data && t.data.data.access_token) {
                        var a = t.data.data.uname, o = t.data.data.upic, n = t.data.data.phone;
                        wx.setStorage({
                            key: "accessToken",
                            data: t.data.data.access_token,
                            success: function(t) {
                                p("userInfo", {
                                    nickName: a,
                                    avatarUrl: o,
                                    phone: n
                                }), l();
                            },
                            fail: function(t) {
                                k.globalData.accessToken = t.data.data.access_token, k.globalData.userInfo = {
                                    nickName: a,
                                    avatarUrl: o,
                                    phone: n
                                }, l();
                            }
                        });
                    } else -1 == t.data.sta && 10 == t.data.errcode ? f() : -1 == t.data.sta && 1e3 == t.data.errcode ? wx.navigateTo({
                        url: "/pages/exception/exception"
                    }) : (r("服务器登录失败，" + t.data.msg), "function" == typeof e && e());
                    s();
                },
                fail: function(t) {
                    s(), r("服务器登录超时，" + t.errMsg), "function" == typeof e && e();
                },
                complete: function() {
                    k.login.status = 0;
                }
            });
        }
    }));
}

function l() {
    for (var t = k.login.cbSuccess, e = t.length, a = 0; a < e; a++) {
        var o = t[a];
        "function" == typeof o && o();
    }
    k.login.cbSuccess = [];
}

function f() {
    for (var t = k.login.cbError, e = t.length, a = 0; a < e; a++) {
        var o = t[a];
        "function" == typeof o && o();
    }
    k.login.cbError = [];
}

function g(t, e, a) {
    wx.login({
        success: function(o) {
            var n = o.code;
            n && (p("code", n), console.log(5555555555), u(function() {
                console.log("登录成功，没执行？"), t.setData({
                    hasUserInfo: !0
                }), "function" == typeof e && e();
            }, function() {
                console.log("登录失败，没执行？"), t.setData({
                    hasUserInfo: !1
                }), "function" == typeof a && a();
            }));
        }
    }), 0 != t.data.hasUserInfo ? v = setTimeout(function() {
        g(t);
    }, 24e4) : clearTimeout(v);
}

function d() {
    var t = h("accessToken");
    if (!t) {
        var e = h("version");
        p("version", e || 0), (t = k.globalData.accessToken) && (p("accessToken", t), p("userInfo", k.globalData.userInfo));
    }
    return t;
}

function p(t, e) {
    try {
        wx.setStorageSync(t, e);
    } catch (a) {
        wx.setStorageSync(t, e);
    }
}

function h(t) {
    try {
        return wx.getStorageSync(t);
    } catch (e) {
        return wx.getStorageSync(t);
    }
}

function w(t, e, a, o) {
    wx.request({
        url: k.globalData.host + "/ads/v1/list",
        method: "GET",
        data: {
            access_token: d(),
            scene: e
        },
        success: function(n) {
            if (-500 != n.data.sta) {
                if (0 == n.data.sta) {
                    var s = n.data.data;
                    if (0 == s.length) return;
                    var r = 0;
                    s.length > 1 && (r = Math.floor(Math.random() * s.length)), t.setData({
                        adsList: [ s[r] ]
                    }), "function" == typeof o && o(), x(s[r], a);
                }
            } else u(function() {
                w(t, e, a, o);
            });
        }
    });
}

function x(t, e) {
    wx.reportAnalytics("xcx_hl_impr", {
        ad_id: t.ad_id,
        ad_name: t.desc,
        from: e
    });
}

function m(t, e) {
    console.log("xcx_hl_click  ==== :"), console.log(t), console.log(e), wx.reportAnalytics("xcx_hl_click", {
        ad_id: t.ad_id,
        ad_name: t.desc,
        from: e
    });
}

var v, k = getApp();

module.exports = {
    formatDate: function(t, e) {
        var a = {
            "M+": (t = isNaN(t) ? t : new Date(t)).getMonth() + 1,
            "d+": t.getDate(),
            "H+": t.getHours(),
            "m+": t.getMinutes(),
            "s+": t.getSeconds(),
            "q+": Math.floor((t.getMonth() + 3) / 3),
            "f+": t.getMilliseconds()
        };
        /(y+)/.test(e) && (e = e.replace(RegExp.$1, (t.getFullYear() + "").substr(4 - RegExp.$1.length)));
        for (var o in a) new RegExp("(" + o + ")").test(e) && (e = e.replace(RegExp.$1, 1 === RegExp.$1.length ? a[o] : ("00" + a[o]).substr(("" + a[o]).length)));
        return e;
    },
    formatNumber: function(t) {
        return (t = t.toString())[1] ? t : "0" + t;
    },
    getDaysLater: function(t) {
        var e = new Date();
        return new Date(e.valueOf() + 864e5 * t);
    },
    getUTCDateInSeconds: t,
    getTimeInSeconds: e,
    getTimeString: function(t) {
        var e = Math.floor(t / 60 / 60);
        e < 10 && (e = "0" + e);
        var a = Math.floor((t - 60 * e * 60) / 60);
        return a < 10 && (a = "0" + a), e + ":" + a;
    },
    isTextEmpty: a,
    isElemInArray: function(t, e) {
        for (var a = 0; a < t.length; a++) if (t[a] == e) return !0;
        return !1;
    },
    getDistance: function(t, e, a, n) {
        var s = o(e), r = o(n), i = s - r, c = o(t) - o(a), u = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(i / 2), 2) + Math.cos(s) * Math.cos(r) * Math.pow(Math.sin(c / 2), 2)));
        return u *= 6378.137, u = Math.round(1e4 * u) / 10;
    },
    showLoading: n,
    hideLoading: s,
    showToast: function(t) {
        setTimeout(function() {
            wx.showToast({
                title: t,
                icon: "success",
                duration: 1e3
            });
        }, 300);
    },
    showFailedToast: r,
    showModelTips: i,
    login: u,
    buttonLogin: c,
    loginAPI: function(t, e) {
        k.login.cbSuccess.push(t), k.login.cbError.push(e), 1 != k.login.status && (k.login.status = 1, 
        n("正在登录"), wx.login({
            success: function(a) {
                var o = a.code;
                wx.getUserInfo({
                    withCredentials: !0,
                    success: function(t) {
                        t.userInfo, wx.request({
                            url: getApp().globalData.host + "/lotto/v1/login_new",
                            data: {
                                code: o,
                                user_data: {}
                            },
                            method: "POST",
                            success: function(t) {
                                if (t.data.data && t.data.data.access_token) {
                                    var a = t.data.data.uname, o = t.data.data.upic, n = t.data.data.phone;
                                    wx.setStorage({
                                        key: "accessToken",
                                        data: t.data.data.access_token,
                                        success: function(t) {
                                            p("userInfo", {
                                                nickName: a,
                                                avatarUrl: o,
                                                phone: n
                                            }), l();
                                        },
                                        fail: function(t) {
                                            k.globalData.accessToken = t.data.data.access_token, k.globalData.userInfo = {
                                                nickName: a,
                                                avatarUrl: o,
                                                phone: n
                                            }, l();
                                        }
                                    });
                                } else -1 == t.data.sta && 10 == t.data.errcode ? f() : -1 == t.data.sta && 1e3 == t.data.errcode ? wx.navigateTo({
                                    url: "/pages/exception/exception"
                                }) : (r("服务器登录失败，" + t.data.msg), "function" == typeof e && e());
                                s();
                            },
                            fail: function(t) {
                                s(), r("服务器登录超时，" + t.errMsg), "function" == typeof e && e();
                            },
                            complete: function() {
                                k.login.status = 0;
                            }
                        });
                    },
                    fail: function(e) {
                        if (k.login.status = 0, "getUserInfo:fail scope unauthorized" == e.errMsg || "getUserInfo:fail auth deny" == e.errMsg || "getUserInfo:fail:scope unauthorized" == e.errMsg || "getUserInfo:fail:auth deny" == e.errMsg) {
                            var a = getCurrentPages(), o = a.length, n = !1;
                            console.log(a);
                            for (var r = 0; r < o; r++) "pages/auth/auth" == a[r].route && (n = !0);
                            if (n) return;
                            wx.navigateTo({
                                url: "/pages/auth/auth"
                            });
                        } else s(), 0 == k.globalData.settingDialog && (wx.openSetting ? (wx.showModal({
                            title: "提示",
                            content: "请授权抽奖小助手使用用户信息，该操作仅会获取您的昵称和头像，请放心授权",
                            confirmText: "去授权",
                            cancelText: "不想授权",
                            confirmColor: "#ff5353",
                            cancelColor: "#999",
                            success: function(e) {
                                k.globalData.settingDialog = !1, e.confirm ? wx.openSetting({
                                    complete: function(e) {
                                        u(t);
                                    }
                                }) : e.cancel && (console.log("用户点击取消"), f());
                            }
                        }), k.globalData.settingDialog = !0) : wx.showModal({
                            title: "提示",
                            content: "由于您未授权，某些功能无法使用",
                            showCancel: !1,
                            confirmText: "我知道了",
                            confirmColor: "#ff5353",
                            success: function(t) {
                                wx.redirectTo({
                                    url: "/pages/auth/auth"
                                });
                            }
                        }));
                    }
                });
            }
        }));
    },
    postFormId: function(t) {
        wx.request({
            url: getApp().globalData.host + "/lotto/v1/formid",
            data: {
                formid: t,
                access_token: h("accessToken")
            },
            method: "POST"
        });
    },
    toIndex: function() {
        wx.switchTab({
            url: "/pages/index/index"
        });
    },
    checkUpgrade: function(t) {
        var e = h("version"), a = getApp().globalData.version, o = getApp().globalData.versionFeature;
        if (e < a) {
            var n = t.data.noticeBoard;
            n.showUpgradeDialog = 1, t.setData({
                noticeBoard: n,
                upgradeFeature: o
            }), p("version", a);
        }
    },
    buttonClicked: function(t) {
        t.setData({
            buttonClicked: !0
        }), setTimeout(function() {
            t.setData({
                buttonClicked: !1
            });
        }, 500);
    },
    dateDifference: function(t, e) {
        var a;
        return t = Date.parse(t), e = Date.parse(e), a = e - t, a = Math.abs(a), Math.floor(a / 864e5);
    },
    getToken: d,
    getSDKVersion: function(t) {
        return t = t ? parseInt(t.replace(/\./g, "")) : 0;
    },
    setStorage: p,
    getStorage: h,
    removeStorage: function(t) {
        try {
            return wx.removeStorageSync(t);
        } catch (e) {
            return wx.removeStorageSync(t);
        }
    },
    getDiffTime: function(a, o) {
        var n = t(a) + e(o) - new Date().getTime() / 1e3, s = "0天";
        if (n > 0) {
            var r = Math.floor(n / 86400), i = Math.floor(n / 3600);
            s = r > 0 ? r + "天" : i > 0 ? Math.floor(n / 3600) + "小时" : Math.floor(n / 60) + "分钟";
        }
        return s;
    },
    checkSession: function(t, e, a) {
        wx.checkSession({
            success: function() {
                console.log("session key 未过期"), h("accessToken") ? (t.setData({
                    hasUserInfo: !0
                }), "function" == typeof e && e()) : g(t, e, a);
            },
            fail: function() {
                console.log("session key 过期了 ========"), h("authButtonClicked") ? (t.setData({
                    hasUserInfo: !0
                }), "function" == typeof e && e()) : g(t, e, a);
            }
        });
    },
    getAds: w,
    toAdDetail: function(t, e) {
        0 == t.type ? wx.navigateToMiniProgram ? wx.navigateToMiniProgram({
            appId: t.appid,
            path: t.url
        }) : i("您的微信版本太低，不支持跳转") : 1 == t.type ? wx.navigateTo({
            url: "/pages/webview/webview?url=" + t.url
        }) : 2 == t.type && wx.navigateTo({
            url: t.url
        }), m(t, e);
    },
    adShow: x,
    adClick: m,
    autoPlay: function(t, e) {
        var a = t.data.swiper;
        a.autoplay = e, t.setData({
            swiper: a
        });
    },
    compareVersion: function(t, e) {
        t = t.split("."), e = e.split(".");
        for (var a = Math.max(t.length, e.length); t.length < a; ) t.push("0");
        for (;e.length < a; ) e.push("0");
        for (var o = 0; o < a; o++) {
            var n = parseInt(t[o]), s = parseInt(e[o]);
            if (n > s) return 1;
            if (n < s) return -1;
        }
        return 0;
    },
    report: function(t) {
        "show" == t.type ? wx.reportAnalytics("page_show", {
            page: t.page,
            position: t.position,
            appid: t.appid
        }) : "click" == t.type && wx.reportAnalytics("page_click", {
            page: t.page,
            position: t.position
        });
    },
    trim: function(t) {
        return t.replace(/^\s+|\s+$/gm, "");
    }
};